<?php
header("Content-Type: application/json");
// header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: Content-Type");

// Get the request data
$data = json_decode(file_get_contents("php://input"));

// Check if the request data has 'username' and 'password'
if (isset($data->username) && isset($data->password)) {
    // Database connection parameters
    $servername = "localhost";
    $username = "root";
    $password = "root";
    $dbname = "fish";

    try {
        // Create a new PDO connection
        $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        // Prepare and execute the SQL query
        $stmt = $conn->prepare("SELECT * FROM user WHERE username = :username AND password = :password");
        $stmt->bindParam(':username', $data->username);
        $stmt->bindParam(':password', $data->password);
        $stmt->execute();

        // Check if there's a matching user
        if ($stmt->rowCount() == 1) {
            $response = array("status" => "success", "username" => $data->username);
        } else {
            $response = array("status" => "failed");
        }
    } catch (PDOException $e) {
        $response = array("status" => "error", "message" => $e->getMessage());
    }
} else {
    $response = array("status" => "error", "message" => "Missing username or password");
}

// Send the JSON response
echo json_encode($response);

?>
